package com.ruoyi.system.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysElectronicMapper;
import com.ruoyi.system.domain.SysElectronic;
import com.ruoyi.system.service.ISysElectronicService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-21
 */
@Service
public class SysElectronicServiceImpl implements ISysElectronicService
{
    @Autowired
    private SysElectronicMapper sysElectronicMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param electronicid 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysElectronic selectSysElectronicByElectronicid(Long electronicid)
    {
        return sysElectronicMapper.selectSysElectronicByElectronicid(electronicid);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysElectronic 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysElectronic> selectSysElectronicList(SysElectronic sysElectronic)
    {
        return sysElectronicMapper.selectSysElectronicList(sysElectronic);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysElectronic 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysElectronic(SysElectronic sysElectronic)
    {
        return sysElectronicMapper.insertSysElectronic(sysElectronic);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysElectronic 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysElectronic(SysElectronic sysElectronic)
    {
        return sysElectronicMapper.updateSysElectronic(sysElectronic);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param electronicids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysElectronicByElectronicids(Long[] electronicids)
    {
        return sysElectronicMapper.deleteSysElectronicByElectronicids(electronicids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param electronicid 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysElectronicByElectronicid(Long electronicid)
    {
        return sysElectronicMapper.deleteSysElectronicByElectronicid(electronicid);
    }

    @Override
    public void create() {
        File file = new File("/www/wwwroot/stock", "setProductInfo.js");
        HashMap<Object, Object> map = new HashMap<>();
        SysElectronic sysElectronic = new SysElectronic();
        StringBuffer sb = new StringBuffer();
        String s="module.exports = {\n" +
                "\n" +
                "  //网站 digikey.com\n" +
                "  diyProducts: [\n";
        sb.append(s);
        sysElectronic.setWebsite("digikey.com");
        List<SysElectronic> digikey = sysElectronicMapper.createJson(sysElectronic);
        sysElectronic.setWebsite("arrow.com");
        List<SysElectronic> arrow = sysElectronicMapper.createJson(sysElectronic);
        for (SysElectronic electronic : digikey) {
            sb.append("    { name: '"+electronic.getName()+"', url: '"+electronic.getUrl()+"' },\n");
        }
        sb.append("  ],\n" +
                "\n" +
                "  //网站 arrow.com\n" +
                "  arrowProducts: [\n");
        for (SysElectronic electronic : arrow) {
            sb.append("    { name: '"+electronic.getName()+"', url: '"+electronic.getUrl()+"' },\n");
        }
        sb.append("  ],\n" +
                "\n" +
                "  //接收通知的人   推送网站：息知\n" +
                "  persons: [\n" +
                "     { name: '温', url: 'https://xizhi.qqoq.net/XZcab3d00a1e245b5dc5ba7a00febe7a47.send' },\n" +
                "     { name: '苏', url: 'https://xizhi.qqoq.net/XZbd5d19667619b5fed1e0d809f15e5b8d.send' },\n" +
                "     { name: '哲', url: 'https://xizhi.qqoq.net/XZ25db08483b4b04c5d56090754853adb5.send' },\n" +
                "  ]\n" +
                "\n" +
                "}");
        try {
            FileUtils.deleteQuietly(file);
            FileUtils.write(file,sb.toString(),"UTF-8", true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String importUser(List<SysElectronic> userList, boolean updateSupport, String operName) {
        {
            if (StringUtils.isNull(userList) || userList.size() == 0)
            {
                throw new ServiceException("导入用户数据不能为空！");
            }
            int successNum = 0;
            int failureNum = 0;
            StringBuilder successMsg = new StringBuilder();
            StringBuilder failureMsg = new StringBuilder();
            for (SysElectronic sysElectronic : userList) {
                this.insertSysElectronic(sysElectronic);
            }
            return "导入成功".toString();
        }
    }
}
